package com.onetech.adrewards.free.adapters;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.objects.NewsDTO;

public class ListNewsAdapter extends BaseAdapter {
	private class AppViewHolder {
		public TextView content;
		public TextView date_created;
		public ImageView iconNavi;
	}

	private Activity activity;
	private List<NewsDTO> listApps;
	private static LayoutInflater inflater = null;

	public ListNewsAdapter(Activity _activity) {
		activity = _activity;
		listApps = new ArrayList<NewsDTO>();
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void updateAppList(List<NewsDTO> _listAppDTOs) {
		listApps.clear();
		listApps.addAll(_listAppDTOs);
		notifyDataSetChanged();
	}

	public void insertAppList(List<NewsDTO> _listAppDTOs) {
		listApps.addAll(_listAppDTOs);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return listApps.size();
	}

	@Override
	public NewsDTO getItem(int arg0) {
		if (listApps == null) {
			return null;
		}
		return listApps.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		AppViewHolder viewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_row_list_news, parent,
					false);
			viewHolder = new AppViewHolder();
			// viewHolder.title = (TextView) convertView
			// .findViewById(R.id.appName);
			viewHolder.content = (TextView) convertView
					.findViewById(R.id.charge_type);
			viewHolder.date_created = (TextView) convertView
					.findViewById(R.id.dateNewsCreated);
			viewHolder.iconNavi = (ImageView) convertView
					.findViewById(R.id.newsImage);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (AppViewHolder) convertView.getTag();
		}
		final NewsDTO temp = listApps.get(position);
		// viewHolder.title.setText(temp.getTitle());

		viewHolder.content.setText(Html.fromHtml(temp.getContents()));

		// viewHolder.content.setSingleLine(true);

		final TextView mContent = viewHolder.content;
		final ImageView mIconNavi=viewHolder.iconNavi;
		viewHolder.content.post(new Runnable() {
			@Override
			public void run() {
				int txtLineCount = mContent.getLineCount();
				if (txtLineCount > 2) {
					mContent.setLines(2);
					mIconNavi.setVisibility(View.VISIBLE);
				}else{
					if(txtLineCount==2){
						mContent.setLines(2);
					}else{
						mContent.setLines(1);	
					}					
					mIconNavi.setVisibility(View.GONE);
				}
			}
		});
		viewHolder.date_created.setText(temp.getCreated_at()==null?"":temp.getCreated_at());
		return convertView;
	}

}
